define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'excourse/orgcate/index' + location.search,
                    add_url: 'excourse/orgcate/add',
                    edit_url: 'excourse/orgcate/edit',
                    del_url: 'excourse/orgcate/del',
                    multi_url: 'excourse/orgcate/multi',
                    table: 'excourse_cate',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                $(".commonsearch-table").find('.btn-success').html('搜索');
                $(".commonsearch-table").find('.btn-default').html('刷新');
                $('.form-commonsearch').attr('autocomplete', 'off');
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'sort', title: __('序号'), operate:false},
                        {field: 'org_cate_id', title: __('Org_cate_id'), operate:false},
                        {field: 'screat', title: __('screat'), operate:false},
                        // {field: 'org_cate_name', title: __('Org_cate_name'), operate:'LIKE'},
                        {
                            field: 'org_cate_name',
                            title: __('Org_cate_name'),
                            visible: false,
                            align: 'left',
                            // operate:'like',
                            searchList: $.getJSON('usechart/searchlist')
                        },
                        {field: 'linkman', title: __('联系人'), operate:false},
                        {field: 'mobile', title: __('Mobile'), operate:false},
                        {field: 'url', title: __('Url'), operate:false},
                        {field: 'start_time', title: __('Start_time'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'),
                            table: table,
                            buttons: [
                                {
                                    name: 'course',
                                    title: __('课程输出管理'),
                                    text:'课程输出管理',
                                    classname: 'btn btn-xs btn-warning btn-addtabs btn-ajax',
                                    // icon: 'fa fa-folder-o',
                                    url: 'excourse/orgcate/course'+ location.search
                                }
                            ],
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: true,
                commonSearch: false,
                search:true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-editone").data("area", ["100%","100%"]);
                    $(".columns-right").hide();
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        course: function () {
            Table.api.init({
                extend: {
                    chapter_url: 'excourse/orgcate/course'+ location.search,
                    table: 'excourse_orgcate_course'
                }
            });
            var table1 = $('#table1');

            table1.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.chapter_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'sort', title: __('序号'),operate:false},
                        {field: 'number', title: __('课程编码'), operate:'LIKE'},
                        //{field: 'id', title: __('课程id'),operate:false},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'teacher.name', title: __('主讲老师'), operate:'LIKE'},
                        {field: 'origin_price', title: __('Origin_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'visitor_origin_price', title: __('Visitor_origin_price') ,operate:false},
                        {field: 'visitor_price', title: __('Visitor_price'),operate:false},
                        //{field: 'buy_numbers', title: __('Buy_numbers'), operate:'>='},
                        {field: 'image', title: __('Image'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin.username', title: __('创建人'), operate:'LIKE'},
                        //{field: 'thumbs', title: __('Thumbs'),operate:false},
                        //{field: 'is_buy', title: __('Is_buy'),operate:false, searchList: {"0":__('Is_buy 0'),"1":__('Is_buy 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},

                        {field: 'operate',
                            title: __('Operate'),
                            table: table1,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'coursedel',
                                    title: __('Delete'),
                                    classname: 'btn btn-xs btn-danger btn-click',
                                    icon: 'fa fa-trash',
                                    click: function (data,row) {
                                        confirmf(row.id);
                                        // Layer.alert("点击按钮执行的事件");
                                    }
                                },
                            ],
                            operate:false,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true
            });

            // 为表格绑定事件
            Table.api.bindevent(table1);
            var confirmf = function(id){
                Layer.confirm("确定删除该课程吗？",
                    {
                        title: '删除课程',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(id,index, layero);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            }
            var submitExportForm = function (id,index, layero) {
                var sys_id = $('#course_id').val();
                var data = {
                    ids: id,
                    org_id:sys_id
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('excourse/orgcate/coursedel'),
                    data: data,
                    pk: 'id'
                }, function (res) {
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    $(".btn-refresh").trigger("click");
                });
            };
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table1);  //所有选中的项
                Layer.confirm("确定删除选中的"+ ids.length +"个课程吗？",
                    {
                        title: '批量删除课程',
                        btn: ["确定", "取消"],
                        yes: function (index, layero) {
                            submitExportForm(ids.join(","), layero, index);
                            return false;
                        },
                        btn2: function (index, layero) {
                            layer.close(index);
                            return false;
                        },
                    });
            });
        },
        courseadd: function () {
            Table.api.init({
                extend: {
                    add_url: 'excourse/orgcate/courseadd'+ location.search,
                    table: 'excourse_orgcate_courseadd'
                }
            });
            var table2 = $('#table2');

            table2.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.add_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'sort', title: __('序号'),operate:false},
                        {field: 'number', title: __('课程编码'), operate:'LIKE'},
                        {field: 'name', title: __('Name'), operate:'LIKE'},
                        {field: 'teacher.name', title: __('主讲老师'), operate:'LIKE'},
                        {field: 'origin_price', title: __('Origin_price'), operate:'BETWEEN'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'visitor_origin_price', title: __('Visitor_origin_price') ,operate:false},
                        {field: 'visitor_price', title: __('Visitor_price'),operate:false},
                        //{field: 'buy_numbers', title: __('Buy_numbers'), operate:'>='},
                        {field: 'image', title: __('Image'),operate:false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'admin.username', title: __('创建人'), operate:'LIKE'},
                        //{field: 'thumbs', title: __('Thumbs'),operate:false},
                        //{field: 'is_buy', title: __('Is_buy'),operate:false, searchList: {"0":__('Is_buy 0'),"1":__('Is_buy 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'),operate:false, searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},

                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
                searchFormVisible: false,
                commonSearch: false,
                search:true
            });
            // 为表格绑定事件
            Table.api.bindevent(table2);
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                var ids = Table.api.selectedids(table2);  //所有选中的项
                submitExportForm(ids.join(","), table2);
                // var page = table2.bootstrapTable('getData');  //当前页面
                // var url = 'excourse/orgcate/bindcourse';
                // Layer.confirm("<form id='check-piliang' class='form-horizontal'  action='" + Fast.api.fixurl(url) + "' method='post'>" +
                //     "<input type='hidden' name='ids' value='' />" +
                //     "</form>",
                //     {
                //         title: '确认添加',
                //         btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)"],
                //         yes: function (index, layero) {
                //             submitExportForm(ids.join(","), layero, index);
                //             return false;
                //         },
                //         btn2: function (index, layero) {
                //             var ids = [];
                //             $.each(page, function (i, j) {
                //                 ids.push(j.id);
                //             });
                //             submitExportForm(ids.join(","), layero, index);
                //
                //             return false;
                //         },
                //     });
            });
            var submitExportForm = function (ids, layero, index) {
                var data = {
                    ids: ids,
                    task_id: $('#task_id').val()
                };
                Fast.api.ajax({
                    url: Fast.api.fixurl('excourse/orgcate/bindcourse'),
                    data: data
                }, function (res) {
                    Layer.alert('添加成功！');
                    Layer.closeAll();
                    Fast.api.close(layero.selector);
                    top.window.$("ul.nav-addtabs .active").trigger("click");
                    // top.window.$("ul.nav-addtabs li.active").find(".fa-remove").trigger("click");
                });
            };
        },
        courseedit: function () {
            Controller.api.bindevent();
        },
        coursedel: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});